package com.pine.activemq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.jms.Queue;

/**
 * @author liucs
 */
@Component
public class JmsComponent {

    JmsMessagingTemplate messagingTemplate;
    @Autowired
    public void setMessagingTemplate(JmsMessagingTemplate messagingTemplate) {
        this.messagingTemplate = messagingTemplate;
    }
    Queue queue;
    @Autowired
    public void setQueue(Queue queue) {
        this.queue = queue;
    }

    /**
     * 发送消息到queue
     * @param msg 消息体
     */
    public void send(Message msg) {
        messagingTemplate.convertAndSend(this.queue, msg);
    }

    /**
     * 消息消费者，订阅的destination为amq
     * @param msg 消息体
     */
    @JmsListener(destination = "amq")
    public void receive(Message msg) {
        System.out.println("receive:" + msg);
    }
}
